﻿using System.Collections.Generic;
using System.Net;
using System.Web.Http;
using StevensReceipts.Domain;
using StevensReceipts.Repository;

namespace StevensReceipts.Web.Areas.Service.Controllers
{
    public class BusinessController : ApiControllerBase
    {
        public BusinessController(IReceiptsDataSource db)
        {
            DataSource = db;
        }

        //GET api/business
        public IEnumerable<Business> Get()
        {
            return DataSource.Businesses.GetAll();
        }

        //GET api/business/allforuser/1
        [ActionName("AllforUser"), HttpGet]
        public IEnumerable<Business> AllforUser(int id)
        {
            IEnumerable<UserPermissionLine> permissions = DataSource.UserPermissions.Get(up => up.UserId == id);
            var businesses = new List<Business>();
            foreach (UserPermissionLine upl in permissions)
            {
                int businessId = upl.BusinessId;
                Business biz = DataSource.Businesses.GetSingle(b => b.Id == businessId);
                businesses.Add(biz);
            }
            return businesses;
        }   

        //GET api/business/1
        public Business Get(int id)
        {
            return DataSource.Businesses.GetSingle(b => b.Id == id);
        }

        // POST api/business
        public void Post(Business value)
        {
            DataSource.Businesses.Add(value);
            DataSource.Save();
        }

        // PUT api/business/5
        public void Put(int id, Business value)
        {
            Business biz = DataSource.Businesses.GetSingle(b => b.Id == id);
            if(biz != null)
            {
                biz.BusinessCountry = value.BusinessCountry;
                biz.BusinessLine = value.BusinessCountry;
                biz.BusinessName= value.BusinessCountry;
                biz.ChartOfAccounts = value.ChartOfAccounts;
                biz.ContactPerson = value.ContactPerson;
                biz.FinancialYearEndDay = value.FinancialYearEndDay;
                biz.FinancialYearEndMonth = value.FinancialYearEndMonth;
                biz.GstNumber = value.GstNumber;
                biz.GstPeriod = value.GstPeriod;
                biz.GstRegistered = value.GstRegistered;
                biz.Modified = value.Modified;
                biz.PhysicalAddress = value.PhysicalAddress;
                biz.PostalAddress = value.PostalAddress;
                biz.TradingName = value.TradingName;
                biz.Type = value.Type;

                DataSource.Save();
            }else
                throw new HttpResponseException(HttpStatusCode.NotFound);
        }

        // DELETE api/business/5
        public void Delete(int id)
        {
            Business biz = DataSource.Businesses.GetSingle(b => b.Id == id);
            if(biz != null)
            {
                DataSource.Businesses.Delete(biz);
                DataSource.Save();
            }
            else
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }
    }
}
